package com.teste.cadastrodeclientes;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListView;

public class ListarActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.listar);
	
	SQLiteDatabase db = openOrCreateDatabase("clientes.db", Context.MODE_PRIVATE, null);
	
	//Criando a tabela dos clientes
	StringBuilder sqlClientes = new StringBuilder();
	sqlClientes.append("CREATE TABLE IF NOT EXISTS clientes(_id INTEGER PRIMARY KEY, nome VARCHAR(30), email VARCHAR(60), cidade VARCHAR(60))");
	db.execSQL(sqlClientes.toString());
	
	
	Cursor cursor = db.rawQuery("SELECT * FROM CLIENTES", null);
	
	//Pega as colunas da tabela
	String[] from = {"_id", "nome", "email"};
	
	//Onde vai jogar na xml
	int[] to = {R.id.txvID, R.id.txvNome, R.id.txvEmail};
	SimpleCursorAdapter ad = new SimpleCursorAdapter(getBaseContext(), R.layout.listar_clientes, cursor, from, to);
	
	ListView ltwDados = (ListView) findViewById(R.id.listDados);
	
	ltwDados.setAdapter(ad);
    }
}
